import QQMapWX from "@/components/qqmap-wx-jssdk.js";
import {
	getregion
} from "@/apis/request";
//创建腾讯地图实例 根据经纬度查询附近地点
var keys = new QQMapWX({
	key: "DF3BZ-JAE6U-QXCVE-27P4U-77OZ6-VGBC7"
});

let globalData = {};
// 小程序模态框
export function dialog(title, content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: title,
			content: content,
			success(res) {
				if (res.confirm) {
					resolve("用户点击确定");
				} else if (res.cancel) {
					reject("用户点击取消");
				}
			}
		});
	});
}

// 小程序提示框
export function Toast(title, duration = 2000) {
	uni.showToast({
		title,
		icon: "none",
		duration
	});
}

// 小程序清理本地缓存
export function clearStorage() {
	uni.showLoading({
		title: "加载中"
	});
	return new Promise((resolve, reject) => {
		uni.clearStorage({
			success(res) {
				resolve(res);
			},
			fail(err) {
				reject(err);
			},
			complete() {
				wx.hideLoading();
			}
		});
	});
}

//小程序图片预览
export function Imgpreview(current, imgList) {
	return new Promise((resolve, reject) => {
		uni.previewImage({
			current: current, // 当前显示图片的http链接
			urls: imgList, // 需要预览的图片http链接列表
			success: res => {
				resolve(res);
			},
			fail: err => {
				reject(err);
			}
		});
	});
}

// 管理小程序用户权限
//#ifdef MP-WEIXIN
export function auth(scope) {
	var scopeList = {
		userInfo: "scope.userInfo",
		userLocation: "scope.userLocation",
		userLocationBackground: "scope.userLocationBackground",
		werun: "scope.werun",
		record: "scope.record",
		writePhotosAlbum: "scope.writePhotosAlbum",
		camera: "scope.camera"
	};

	return new Promise((resolve, reject) => {
		// 可以通过 wx.getSetting 先查询一下用户是否授权了这个 scope
		uni.getSetting({
			success: res => {
				console.log("授权", res);
				resolve(res)
				if (!res.authSetting[scopeList[scope]]) {
					console.log(scope);
					//微信小程序授权
					wx.authorize({
						scope: scopeList[scope],
						success() {
							// 用户已经同意小程序使用，后续调用接口不会弹窗询问
							resolve();
						},
						fail(err) {
							// 用户取消授权
							reject(err);
						}
					});
				} else {
					console.log("用户已授权");
					resolve();
				}
			}
		});
	});
}
//#endif
// 打开设置用户授权页
export function openSetting(callback, failback) {
	uni.openSetting({
		success: res => {
			if (callback) return callback(res);
		},
		fail: err => {
			if (failback) return failback(err);
		}
	});
}

// 获取省市地区列表;
export async function getcityList(regionGrade) {
	//1 省 2 市 3 区县 4 街道
	const list = await getregion({
		regionGrade: regionGrade
	});
	if (regionGrade == 1) {
		var province = [];
		province.push(
			...list.map(item => {
				return {
					id: item.id,
					text: item.regionName
				};
			})
		);
		wx.setStorage({
			key: "position",
			data: {
				province: JSON.stringify(province)
			}
		});
		// this.cityList[0].values.push(...this.province);
	} else if (regionGrade == 2) {
		const {
			cityName
		} = globalData;
		var cityList = [];
		cityList.push(
			...list.map(item => {
				return {
					id: item.id,
					regionPid: item.regionPid,
					text: item.regionName
				};
			})
		);
		var cityId = cityList.filter(item => {
			return item.text == cityName;
		});
		uni.setStorageSync("cityId", cityId[0].id);
		uni.setStorageSync("cityList", JSON.stringify(cityList));
		globalData.cityList = cityList;
		globalData.cityId = cityId[0].id;
	}
}

// 地址解析;根据城市名返回坐标
export function geocoder(params, callback, failback) {
	keys.geocoder({
		address: {
			address: params
		},
		success: res => {
			const location = res.result.location;
			if (callback) return callback(location);
		},
		fail: err => {
			if (failback) return failback(err);
		}
	});
}

// 逆地址解析;根据坐标返回城市名称
export function reverseGeocoder(latitude, longitude, callback, failback) {
	keys.reverseGeocoder({
		location: {
			latitude: latitude,
			longitude: longitude
		},
		success: res => {
			//获取详细地址
			const cityName = res.result.ad_info.city;
			const cityId = res.result.ad_info.adcode.substring(0, 4) + "00";
			const city = {
				id: cityId,
				title: cityName
			};
			uni.setStorageSync("city", city);
			if (callback) return callback(city);
		},
		fail: err => {
			if (failback) return failback(err);
		}
	});
}

// 小程序定位
export function getLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: "gcj02",
			success(res) {
				resolve(res);
			},
			fail: err => {
				reject(err);
			}
		});
	});
}